#include <QtGui/QApplication>
#include <QtGui/QMessageBox>
#include <QtSql>
#include "mainwindow.h"

bool startup;

bool createConnection()
{
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("students.db");

    if (!db.open()) {
        QMessageBox::warning(0, QObject::tr("Database Error"),
                             db.lastError().text());
        return false;
    }
    return true;
}


void createFakeData()
{
    QSqlQuery query;
    query.exec("DROP TABLE students");

    query.exec("CREATE TABLE students ("
               "id INTEGER PRIMARY KEY AUTOINCREMENT, "
               "number INTEGER NOT NULL, "
               "class VARCHAR(10) NOT NULL, "
               "name VARCHAR(40) NOT NULL, "
               "sentry1 INTEGER, "
               "sentry2 INTEGER, "
               "sentry3 INTEGER, "
               "sentry4 INTEGER, "
               "sentryT INTEGER, "
               "sentryP INTEGER )");

    query.exec("INSERT INTO students (number, class, name, sentry1, sentry2, sentry3, sentry4, sentryT, sentryP)"
               "VALUES (10188, '10-L', 'Omer Faruk ORUC'    , 0, 0, 0, 0, 0, 0)");

    query.exec("INSERT INTO students (number, class, name, sentry1, sentry2, sentry3, sentry4, sentryT, sentryP)"
               "VALUES (10202, '10-K', 'Abdussamed KARAKAS' , 0, 0, 0, 0, 0, 0)");

    query.exec("INSERT INTO students (number, class, name, sentry1, sentry2, sentry3, sentry4, sentryT, sentryP)"
               "VALUES (10171, '10-D', 'Muharrem SEKEROGLU' , 0, 0, 0, 0, 0, 0)");

    query.exec("INSERT INTO students (number, class, name, sentry1, sentry2, sentry3, sentry4, sentryT, sentryP)"
               "VALUES (10225, '10-L', 'Numan FIDAN'        , 0, 0, 0, 0, 0, 0)");

    query.exec("INSERT INTO students (number, class, name, sentry1, sentry2, sentry3, sentry4, sentryT, sentryP)"
               "VALUES (10160, '10-H', 'Murat Furkan Gazi'  , 0, 0, 0, 0, 0, 0)");

    query.exec("INSERT INTO students (number, class, name, sentry1, sentry2, sentry3, sentry4, sentryT, sentryP)"
               "VALUES (10164, '10-H', 'Yusuf Amir TEZCAN'  , 0, 0, 0, 0, 0, 0)");

    query.exec("INSERT INTO students (number, class, name, sentry1, sentry2, sentry3, sentry4, sentryT, sentryP)"
               "VALUES (10143, '10-K', 'Furkan UZUMCU'      , 0, 0, 0, 0, 0, 0)");

    query.exec("INSERT INTO students (number, class, name, sentry1, sentry2, sentry3, sentry4, sentryT, sentryP)"
               "VALUES (10010, '10-L', 'Ertugrul KAR'       , 0, 0, 0, 0, 0, 0)");

    query.exec("INSERT INTO students (number, class, name, sentry1, sentry2, sentry3, sentry4, sentryT, sentryP)"
               "VALUES (10099, '10-K', 'Huseyin ZENGIN'     , 0, 0, 0, 0, 0, 0)");

    query.exec("INSERT INTO students (number, class, name, sentry1, sentry2, sentry3, sentry4, sentryT, sentryP)"
               "VALUES (10146, '10-L', 'Burak KILINC'       , 0, 0, 0, 0, 0, 0)");

    query.exec("INSERT INTO students (number, class, name, sentry1, sentry2, sentry3, sentry4, sentryT, sentryP)"
               "VALUES (10042, '10-H', 'Utku AYDIN'         , 0, 0, 0, 0, 0, 0)");

    query.exec("INSERT INTO students (number, class, name, sentry1, sentry2, sentry3, sentry4, sentryT, sentryP)"
               "VALUES (10004, '10-H', 'Nevzat YILMAZ'      , 0, 0, 0, 0, 0, 0)");

    query.exec("INSERT INTO students (number, class, name, sentry1, sentry2, sentry3, sentry4, sentryT, sentryP)"
               "VALUES (10199, '10-D', 'Mucahit TASDELEN'   , 0, 0, 0, 0, 0, 0)");

    query.exec("CREATE TABLE settings ( id INTEGER PRIMARY KEY AUTOINCREMENT,  "
               "name VARCHAR(30),"
               "day INTEGER,     "
               "year INTEGER,    "
               "month INTEGER   )" );

    query.exec("CREATE TABLE roster ( id INTEGER PRIMARY KEY AUTOINCREMENT,  "
               "dayName           VARCHAR(30)  ,  "
               "firstFloorFirst   VARCHAR(30)  ,  "
               "firstFloorSecond  VARCHAR(30)  ,  "
               "secondFloorFirst  VARCHAR(30)  ,  "
               "secondFloorSecond VARCHAR(30)    )");
    //Monday
    query.exec("INSERT INTO roster (dayName)"
               "VALUES (  'monday1'      )");
    query.exec("INSERT INTO roster (dayName)"
               "VALUES (  'monday2'      )");
    query.exec("INSERT INTO roster (dayName)"
               "VALUES (  'monday3'      )");
    query.exec("INSERT INTO roster (dayName)"
               "VALUES (  'monday4'      )");

    //Tuesday
    query.exec("INSERT INTO roster (dayName)"
               "VALUES (  'tuesday1'     )");
    query.exec("INSERT INTO roster (dayName)"
               "VALUES (  'tuesday2'     )");
    query.exec("INSERT INTO roster (dayName)"
               "VALUES (  'tuesday3'     )");
    query.exec("INSERT INTO roster (dayName)"
               "VALUES (  'tuesday4'     )");

    //Wednesday
    query.exec("INSERT INTO roster (dayName)"
               "VALUES (  'wednesday1'   )");
    query.exec("INSERT INTO roster (dayName)"
               "VALUES (  'wednesday2'   )");
    query.exec("INSERT INTO roster (dayName)"
               "VALUES (  'wednesday3'   )");
    query.exec("INSERT INTO roster (dayName)"
               "VALUES (  'wednesday4'   )");

    //Thursday
    query.exec("INSERT INTO roster (dayName)"
               "VALUES (  'thursday1'    )");
    query.exec("INSERT INTO roster (dayName)"
               "VALUES (  'thursday2'    )");
    query.exec("INSERT INTO roster (dayName)"
               "VALUES (  'thursday3'    )");
    query.exec("INSERT INTO roster (dayName)"
               "VALUES (  'thursday4'    )");

    //Friday
    query.exec("INSERT INTO roster (dayName)"
               "VALUES (  'friday1'      )");
    query.exec("INSERT INTO roster (dayName)"
               "VALUES (  'friday2'      )");
    query.exec("INSERT INTO roster (dayName)"
               "VALUES (  'friday3'      )");
    query.exec("INSERT INTO roster (dayName)"
               "VALUES (  'friday4'      )");

    //Saturday
    query.exec("INSERT INTO roster (dayName)"
               "VALUES (  'saturday1'    )");
    query.exec("INSERT INTO roster (dayName)"
               "VALUES (  'saturday2'    )");
    query.exec("INSERT INTO roster (dayName)"
               "VALUES (  'saturday3'    )");
    query.exec("INSERT INTO roster (dayName)"
               "VALUES (  'saturday4'    )");

    //Sunday
    query.exec("INSERT INTO roster (dayName)"
               "VALUES (  'sunday1'      )");
    query.exec("INSERT INTO roster (dayName)"
               "VALUES (  'sunday2'      )");
    query.exec("INSERT INTO roster (dayName)"
               "VALUES (  'sunday3'      )");
    query.exec("INSERT INTO roster (dayName)"
               "VALUES (  'sunday4'      )");
}


int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    bool create = !QFile::exists("students.db");
    if (!createConnection())
        return 1;
    if (create)
        createFakeData();

    MainWindow w;
    w.show();
    w.resize(200,200);

    if(create)
        w.getDate();

    return a.exec();
}
